package pers.hongze.test.service.impl;

import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import pers.hongze.test.mapper.AdminRoleMapper;
import pers.hongze.test.pojo.AdminRole;
import pers.hongze.test.service.AdminRoleService;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class AdminRoleServiceImpl implements AdminRoleService {

    @Autowired
    private AdminRoleMapper adminRoleMapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void saveAdminRole(AdminRole adminRole) throws Exception {
        adminRoleMapper.insert(adminRole);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateAdminRole(AdminRole adminRole) {
        adminRoleMapper.updateByPrimaryKey(adminRole);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteAdminRole(String adminRoleId) {
        adminRoleMapper.deleteByPrimaryKey(adminRoleId);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public AdminRole queryAdminRoleById(String adminRoleId) {
        return adminRoleMapper.selectByPrimaryKey(adminRoleId);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<AdminRole> queryAdminRoleList(AdminRole adminRole) {
        Example example = new Example(AdminRole.class);
        Example.Criteria criteria = example.createCriteria();

//        if (!StringUtils.isEmptyOrWhitespace(user.getUsername())) {
////			criteria.andEqualTo("username", user.getUsername());
//            criteria.andLike("username", "%" + user.getUsername() + "%");
//        }
//
//        if (!StringUtils.isEmptyOrWhitespace(user.getNickname())) {
//            criteria.andLike("nickname", "%" + user.getNickname() + "%");
//        }

        List<AdminRole> adminRoleList = adminRoleMapper.selectByExample(example);

        return adminRoleList;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<AdminRole> queryAdminRoleListPaged(AdminRole adminRole, Integer page, Integer pageSize) {
        // 开始分页
        PageHelper.startPage(page, pageSize);

        Example example = new Example(AdminRole.class);
        Example.Criteria criteria = example.createCriteria();

//        if (!StringUtils.isEmptyOrWhitespace(adminRole.getName())) {
//            criteria.andLike("name", "%" + adminRole.getName() + "%");
//        }

        example.orderBy("id").desc();
        List<AdminRole> adminRoleList = adminRoleMapper.selectByExample(example);

        return adminRoleList;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public AdminRole queryAdminRoleByIdCustom(String adminRoleId) {
        return null;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void saveAdminRoleTransactional(AdminRole adminRole) {
        adminRoleMapper.insert(adminRole);

        int a = 1 / 0;

        adminRole.setDescription("1");
        adminRoleMapper.updateByPrimaryKeySelective(adminRole);
    }
}
